﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Diagnostics;

namespace ALDIOrganizer
{
    [Serializable]
    public class Staff : ICloneable
    {
        private List<EmployeeInRota> _workers;
        public IEnumerable<EmployeeInRota> getWorkers
        {
            get
            {
                foreach (var bloke in _workers)
                {
                    yield return bloke;
                }                
            }
        }

        public EmployeeInRota GetEmployee(Int64 id)
        {
            var query = from worker in getWorkers
                        where worker.Employee.ID == id
                        select worker;
            return query.SingleOrDefault();
        }

        private Rota Parent;

        public Staff(Rota pParent)
        {
            Parent = pParent;
            Refresh();
        }

        public void RefreshEmployee(Int64 pEmpID)
        {
            GetEmployee(pEmpID).RecalculateContractsHolidaysAndPosition(
                true,
                false,
                false,
                Parent.Monday);
            GetEmployee(pEmpID).RecalculateHours(
                (Parent as Rota).GetShiftsForEmployee(pEmpID));
        }
        /// <summary>
        /// refreshes the info. should be called after every modification of any data table.
        /// </summary>
        public IEnumerable<Int64> Refresh()
        {            
            _workers = new List<EmployeeInRota>();

            // gets all emps_from_database from the CurrentFile (DataRow array)
            var emps_from_database = SQLiteAccess.GetEmployees();

            // for each one of those emps_from_database...
            foreach (var worker in Employee.ToEmployees(emps_from_database))
            {
                var worker_in_rota = new EmployeeInRota(worker, Parent);
                worker_in_rota.RecalculateContractsHolidaysAndPosition(
                    true,
                    true,
                    true,
                    Parent.Monday);
                // does he work at all On that week? is he employed? if yes...
                if(worker_in_rota.ContractStatuses.Key[7])                
                {                   
                    worker_in_rota.display = worker_in_rota.ToString();                    
                    _workers.Add(worker_in_rota);
                }
            }
            _workers.Sort();
                        
            return (from worker in getWorkers
                   select worker.Employee.ID);
        }


        #region ICloneable Members

        public object Clone()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
